home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 260_01 / sz.c < prev    next >
Text File  |  1988-02-23  |  40KB  |  1,636 lines

  1. #define VERSION "sz 1.23 01-15-87"
  2. #define PUBDIR "/usr/spool/uucppublic"
  3.  
  4. /*% cc -O -K -i -DCRCTABLE -DREADCHECK sz.c -lx -o sz; size sz
  5.  
  6.  * sz.c By Chuck Forsberg
  7.  *      MS-DOS version by Francois Bergeon
  8.  *
  9.  *      cc -O sz.c -o sz                USG (SYS III/V) Unix
  10.  *      cc -O -DSVR2 sz.c -o sz         Sys V Release 2 with non-blocking input
  11.  *                                      Define to allow reverse channel checking
  12.  *      cc -O -DV7  sz.c -o sz          Unix Version 7, 2.8 - 4.3 BSD
  13.  *      cl rz.c -link stty              MS-DOS, MS-C 4.00+
  14.  *
  15.  *      ln sz sb                        USG & V7
  16.  *
  17.  *              define CRCTABLE to use table driven CRC
  18.  *
  19.  *  ******* Some systems (Venix, Coherent, Regulus) do not *******
  20.  *  ******* support tty raw mode read(2) identically to    *******
  21.  *  ******* Unix. ONEREAD must be defined to force one     *******
  22.  *  ******* character reads for these systems.             *******
  23.  *
  24.  * A program for Unix to send files and commands to computers running
  25.  *  Professional-YAM, PowerCom, YAM, IMP, or programs supporting Y/XMODEM.
  26.  *
  27.  *  Sz uses buffered I/O to greatly reduce CPU time compared to UMODEM.
  28.  *
  29.  *  USG UNIX (3.0) ioctl conventions courtesy Jeff Martin
  30.  *
  31.  *  MS-DOS Adaptation 10-19-87 for the MS-C compiler rev 4.00+
  32.  *  using Francois Bergeon's tty library
  33.  */
  34.  
  35.  
  36. char *substr(), *getenv();
  37.  
  38. #define LOGFILE "/tmp/szlog"
  39.  
  40. #include <stdio.h>
  41. #include <signal.h>
  42. #include <setjmp.h>
  43. #include <ctype.h>
  44.  
  45. #ifdef MSDOS
  46. #include <time.h>
  47. #define CRCTABLE
  48. #define JAN1ST70  315532800      /* difference in seconds between */
  49.                                  /* Jan 1st 1980 & Jan 1st 1970   */
  50. #endif
  51.  
  52. #define PATHLEN 256
  53. #define OK 0
  54. #define FALSE 0
  55. #define TRUE 1
  56. #define ERROR (-1)
  57.  
  58. #define HOWMANY 2
  59. int Zmodem = 0;         /* ZMODEM protocol requested */
  60. unsigned Baudrate;
  61. int Fromcu = 0;         /* Were called from cu or yam */
  62. #include "rbsb.c"       /* most of the system dependent stuff here */
  63.  
  64. /*
  65.  * Attention string to be executed by receiver to interrupt streaming data
  66.  *  when an error is detected.  A pause (0336) may be needed before the
  67.  *  ^C (03) or after it.
  68.  */
  69. #ifdef READCHECK
  70. char Myattn[] = { 0 };
  71. #else
  72. #ifdef USG
  73. char Myattn[] = { 03, 0336, 0 };
  74. #else
  75. char Myattn[] = { 0 };
  76. #endif
  77. #endif
  78.  
  79. FILE *in;
  80.  
  81. /* Ward Christensen / CP/M parameters - Don't change these! */
  82. #define ENQ 005
  83. #define CAN ('X'&037)
  84. #define XOFF ('s'&037)
  85. #define XON ('q'&037)
  86. #define SOH 1
  87. #define STX 2
  88. #define EOT 4
  89. #define ACK 6
  90. #define NAK 025
  91. #define CPMEOF 032
  92. #define WANTCRC 0103    /* send C not NAK to get crc not checksum */
  93. #define WANTG 0107      /* Send G not NAK to get nonstop batch xmsn */
  94. #define TIMEOUT (-2)
  95. #define RCDO (-3)
  96. #define RETRYMAX 10
  97. #define SECSIZ 128      /* cp/m's Magic Number record size */
  98. #define KSIZE 1024
  99.  
  100. char Lastrx;
  101. char Crcflg;
  102. int Wcsmask = 0377;
  103. int Verbose = 0;
  104. int Modem = 0;          /* MODEM - don't send pathnames */
  105. int Restricted = 0;     /* restricted; no /.. or ../ in filenames */
  106. int Quiet = 0;          /* overrides logic that would otherwise set verbose */
  107. int Ascii = 0;          /* Add CR's for brain damaged programs */
  108. int Fullname = 0;       /* transmit full pathname */
  109. int Unlinkafter = 0;    /* Unlink file after it is sent */
  110. int Dottoslash = 0;     /* Change foo.bar.baz to foo/bar/baz */
  111. int firstsec;
  112. int errcnt = 0;         /* number of files unreadable */
  113. int blklen = SECSIZ;    /* length of transmitted records */
  114. int Optiong;            /* Let it rip no wait for sector ACK's */
  115. int Noeofseen;
  116. int Totsecs;            /* total number of sectors this file */
  117. char txbuf[KSIZE];
  118. int Filcnt = 0;         /* count of number of files opened */
  119. int Lfseen = 0;
  120. unsigned Rxbuflen = 16384;      /* Receiver's max buffer length */
  121. int Tframlen = 0;       /* Override for tx frame length */
  122. int blkopt = 0;         /* Override value for zmodem blklen */
  123. int Rxflags = 0;
  124. int Wantfcs32 = TRUE;   /* want to send 32 bit FCS */
  125. char Lzconv;            /* Local ZMODEM file conversion request */
  126. char Lzmanag;           /* Local ZMODEM file management request */
  127. char Lztrans;
  128. char zconv;             /* ZMODEM file conversion request */
  129. char zmanag;            /* ZMODEM file management request */
  130. char ztrans;            /* ZMODEM file transport request */
  131. int Command;            /* Send a command, then exit. */
  132. char *Cmdstr;           /* Pointer to the command string */
  133. int Cmdtries = 11;
  134. int Cmdack1;            /* Rx ACKs command, then do it */
  135. int Exitcode;
  136. int Testattn;           /* Force receiver to send Attn, etc with qbf. */
  137. char *qbf="The quick brown fox jumped over the lazy dog's back 1234567890\r\n";
  138. long Lastread;          /* Beginning offset of last buffer read */
  139. int Lastc;              /* Count of last buffer read or -1 */
  140. int Dontread;           /* Don't read the buffer, it's still there */
  141.  
  142. #ifndef MSDOS
  143. jmp_buf tohere;         /* For the interrupt on RX timeout */
  144. #endif
  145.  
  146. jmp_buf intrjmp;        /* For the interrupt on RX CAN */
  147.  
  148. /* called by signal interrupt or terminate to clean things up */
  149. bibi(n)
  150.    {
  151.    canit();
  152.    fflush(stdout);
  153.    mode(0);
  154.    fprintf(stderr, "sz: caught signal %d; exiting\n", n);
  155. #ifndef MSDOS
  156.    if (n == SIGQUIT)
  157.       abort();
  158. #endif
  159.    exit(128+n);
  160.    }
  161. /* Called when Zmodem gets an interrupt (^X) */
  162.  
  163. onintr()
  164.    {
  165.    signal(SIGINT, SIG_IGN);
  166.    longjmp(intrjmp, -1);
  167.    }
  168.  
  169. #ifndef MSDOS
  170. #define sendline(c) putchar(c & Wcsmask)
  171. #define xsendline(c) putchar(c)
  172.  
  173. flushmo()
  174.    {
  175.    fflush(stdout);
  176.    }
  177.  
  178. #else /* MSDOS */
  179.  
  180. #define sendline(c) xsendline(c & Wcsmask)
  181.  
  182. xsendline(c)
  183. int c;
  184.    {
  185.    tty_write(iofd, &c, 1);
  186.    }
  187.  
  188. flushmo()
  189.    {
  190.    }
  191. #endif
  192.  
  193. #define ZKER
  194. int Zctlesc;    /* Encode control characters */
  195. int Nozmodem = 0;       /* If invoked as "sb" */
  196. char *Progname = "sz";
  197. #include "zm.c"
  198.  
  199.  
  200. main(argc, argv)
  201. char *argv[];
  202.    {
  203.    char *cp;
  204.    int npats;
  205.    int agcnt;
  206.    char **agcv;
  207.    char **patts;
  208.    static char xXbuf[BUFSIZ];
  209.  
  210.    if ((cp = getenv("ZNULLS")) && *cp)
  211.       Znulls = atoi(cp);
  212.    if ((cp = getenv("SHELL")) && (substr(cp, "rsh") || substr(cp, "rksh")))
  213.       Restricted = TRUE;
  214. #ifndef MSDOS
  215.    chkinvok(argv[0]);
  216. #else
  217.    Progname = "sz";
  218. #endif
  219.  
  220.    Rxtimeout = 600;
  221.    npats = 0;
  222.    if (argc < 2)
  223.       usage();
  224.    setbuf(stdout, xXbuf);          
  225.    while (--argc)
  226.       {
  227.       cp = *++argv;
  228.       if (*cp++ == '-' && *cp)
  229.          {
  230.          while (*cp)
  231.             {
  232.             switch (*cp++)
  233.                {
  234.                case '+':
  235.                   Lzmanag = ZMAPND;
  236.                   break;
  237. #ifndef MSDOS
  238.                case '1':
  239.                   iofd = 1;
  240.                   break;
  241. #ifdef CSTOPB
  242.                case '2':
  243.                   Twostop = TRUE;
  244.                   break;
  245. #endif
  246. #else
  247.                case '2':
  248.                   port = "COM2";
  249.                   break;
  250.                case 's':
  251.                   Baudrate = atoi(cp);
  252.                   if ((speed = getspeed(Baudrate)) < 0)
  253.                      usage();
  254.                   cp = '\0';
  255.                   break;
  256.                case 'Y':
  257.                   Nozmodem = TRUE;
  258.                   break;
  259. #endif
  260.                case '7':
  261.                   Wcsmask=0177;
  262.                   break;
  263.                case 'a':
  264.                   Lzconv = ZCNL;
  265.                   Ascii = TRUE;
  266.                   break;
  267.                case 'b':
  268.                   Lzconv = ZCBIN;
  269.                   break;
  270.                case 'C':
  271.                   if (--argc < 1)
  272.                      usage();
  273.                   Cmdtries = atoi(*++argv);
  274.                   break;
  275.                case 'i':
  276.                   Cmdack1 = ZCACK1;
  277.                /* **** FALL THROUGH TO **** */
  278.                case 'c':
  279.                   if (--argc != 1)
  280.                      usage();
  281.                   Command = TRUE;
  282.                   Cmdstr = *++argv;
  283.                   break;
  284.                case 'd':
  285.                   ++Dottoslash;
  286.